package cn.ingrun.ingrunutils.exception;

import cn.ingrun.ingrunutils.responseUtil.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局统一异常处理增强
 **/
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 自定义异常处理
     **/
    @ExceptionHandler(value = ResponseException.class)
    public ResponseUtil jsonApiErrorHandler(ResponseException e) {
        logger.warn(e.getMessage());
        return new ResponseUtil(e.getResponseModel(), HttpStatus.valueOf( e.getResponseModel().getCode()));
    }

}
